package com.moses.transformer;

import com.moses.tools.ClassByteTool;

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;

public class FooPostTransformer implements ClassFileTransformer {

    @Override
    public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {

        if (!className.endsWith("com/moses/beans/Foo")) {
            return null;
        }

        String name = className.replaceAll("/", ".");

        byte[] bytes = ClassByteTool.addTimeHint(name, "sayHello", "System.out.println(\"Hello,\"+$1);\n");

        if (bytes != null && bytes.length > 0) {
            return bytes;
        }
        return null;
    }

}
